﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
namespace HF.Permission.Data
{
    /// <summary>
    /// 数据访问类PurviewData。
    /// </summary>
    public class PurviewData
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "PrvID", "GroupId", "OperationId" };
        private string tableName = "HF_Purview";
        private string keyField = "PrvID";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(PurviewEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@PrvID", entity.PrvID, typeof(string));
            sqlDataItem.AppendParameter("@GroupId", entity.GroupId, typeof(string));
            sqlDataItem.AppendParameter("@OperationId", entity.OperationId, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public PurviewEntity GetPurviewEntity(DataRow dr)
        {
            PurviewEntity entity = new PurviewEntity();

            entity.PrvID = dr["PrvID"].ToString();
            entity.GroupId = dr["GroupId"].ToString();
            entity.OperationId = dr["OperationId"].ToString();
            return entity;
        }

        #endregion
        #region  构造函数
        public PurviewData()
        { }
        public static PurviewData GetInstance()
        {
            return new PurviewData();
        }
        #endregion
        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(PurviewEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(PurviewEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string prvID)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, prvID);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="prvID">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string prvID)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, prvID);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="PrvID">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetPurviewTable(string prvID)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, prvID);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="prvID">主键Id</param>
        /// <returns>DataTable</returns>
        public PurviewEntity GetPurviewEntity(string prvID)
        {
            DataTable dt = GetPurviewTable(prvID);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetPurviewEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion

        #region  扩展方法
        //此处添加扩展方法
        /// <summary>
        ///  判断是否有权限
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="operationId"></param>
        /// <returns></returns>
        public  bool IsPower(string userId, string operationId)
        {
            if (userId.ToLower() == "bpmadmin" || userId.ToLower() == "bpmadmin") return true;
            string tmpSql = "select * from HF_PowerView where UserId=@UserId and OperationId=@OperationId";
            SqlDataItem sqlItem = new SqlDataItem();
            try
            {
                sqlItem.AppendParameter("@UserId", userId);
                sqlItem.AppendParameter("@OperationId", operationId);
                sqlItem.CommandText = tmpSql;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.RecordExists(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 判断权限在该组中是否已存在
        /// </summary>
        /// <param name="operationId">权限码,可以是模块Id、功能Id和操作Id</param>
        /// <param name="groupId">权限组id</param>
        /// <returns>是否存在</returns>
        public  bool IsPowerExistInGroup(string operationId, string groupId)
        {
            string tmpSql = "select * from HF_PowerView where OperationId like @OperationId  and GroupId=@groupId";
            SqlDataItem sqlItem = new SqlDataItem();
            try
            {
                sqlItem.AppendParameter("@GroupId", groupId);
                sqlItem.AppendParameter("@OperationId", operationId + "%");
                sqlItem.CommandText = tmpSql;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.RecordExists(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 为权限组设置权限
        /// </summary>
        /// <param name="operationId">权限码,可以是模块Id、功能Id和操作Id</param>
        /// <param name="groupId">权限组Id</param>
        public  bool SetPowerToGroup(string operationId, string groupId)
        {
            SqlDataItem sqlItem1 = new SqlDataItem();
            SqlDataItem sqlItem2 = new SqlDataItem();
            SqlDataItemArray sqlItemAry = new SqlDataItemArray();

            try
            {
                //先删除原来的
                sqlItem1.CommandText = "delete HF_Purview where GroupId=@GroupId and OperationId like @operationId";
                sqlItem1.AppendParameter("@operationId", operationId + "%");
                sqlItem1.AppendParameter("@groupId", groupId);

                sqlItem2.CommandText = "insert into HF_Purview(GroupId,OperationId) select @groupId,OperationId from HF_Operation " +
                                          " where OperationId like @operationId";
                sqlItem2.AppendParameter("@operationId", operationId + "%");
                sqlItem2.AppendParameter("@groupId", groupId);

                sqlItemAry.AppendSqlItem(sqlItem1);
                sqlItemAry.AppendSqlItem(sqlItem2);

                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQueryArray(sqlItemAry);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 移除权限组的权限
        /// </summary>
        /// <param name="operationId">权限码,可以是模块Id、功能Id和操作Id</param>
        /// <param name="groupId">权限组Id</param>
        public  bool RemovePowerFromGroup(string operationId, string groupId)
        {
            SqlDataItem sqlItem1 = new SqlDataItem();
            ClientDBAgent agent = new ClientDBAgent();
            try
            {
                sqlItem1.CommandText = "delete HF_Purview where GroupId=@GroupId and OperationId like @operationId";
                sqlItem1.AppendParameter("@operationId", operationId + "%");
                sqlItem1.AppendParameter("@groupId", groupId);
                agent.ExecuteNonQuery(sqlItem1);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion
        #endregion
    }
}

